<%@page import="java.net.URLEncoder"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登录成功</title>
</head>
<body>
  <h1>登录成功</h1>
  <hr>
  <br>
  <br>
  <%
     // 首先判断用户是否选择了记住登录状态
     String[] isUseCookies = request.getParameterValues("isUseCookie");
     if(isUseCookies != null && isUseCookies.length > 0) {
    	 // 把用户名和密码保存在Cookie对象里面
    	 request.setCharacterEncoding("utf-8");
    	 String username = URLEncoder.encode(request.getParameter("username"), "utf-8");
    	 // 使用URLEncoder解决无法在Cookie中保存中文字符串问题
    	 String password = URLEncoder.encode(request.getParameter("password"), "utf-8");
    	 
    	 
    	 Cookie usernameCookie = new Cookie("username", username);
    	 Cookie passwordCookie = new Cookie("password", password);
    	 
    	 // 设置最大生存期限为10天
    	 usernameCookie.setMaxAge(864000);
    	 passwordCookie.setMaxAge(86400);
    	 
    	 response.addCookie(usernameCookie);
    	 response.addCookie(passwordCookie);
     } else {
    	 Cookie[] cookies = request.getCookies();
    	 if(cookies != null && cookies.length > 0) {
    		 for(Cookie c: cookies) {
    			 if(c.getName().equals("username") || c.getName().equals("password")) {
    				 c.setMaxAge(0); // 设置cookie失效
    				 response.addCookie(c); // 重新保存。
    			 }
    		 }
    	 }
     }
  %>
  <a href="users.jsp">查看用户信息</a>
</body>
</html>